package java学习.JUC;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * **************
 * 项目名称: 蓝桥杯 <br/>
 * 文件名称:  <br/>
 * 文件描述: 这里添加您的类文件描述，说明当前文件要包含的功能。 <br/>
 * 文件创建：刘世锦 <br/>
 * 创建时间: 2022/6/2 <br/>
 *
 * @version v1.0 <br/>
 * @update [序号][日期YYYY-MM-DD][更改人姓名][变更描述]<br/>
 * *************
 */
class shareResource{

    private int flag = 1;
    private Lock lock = new ReentrantLock();
    private Condition A = lock.newCondition();
    private Condition B = lock.newCondition();
    private Condition C = lock.newCondition();

    public void print2(int loop) throws InterruptedException {
        lock.lock();
        try {
            if (flag!=1){
                A.await();
            }

            for (int i = 1; i <=2 ; i++) {

                System.out.println( Thread.currentThread().getName()+"   第"+i+"次" +"第"+loop+" 轮" );

            }
            flag=2;

            B.signal();
        }finally {
            lock.unlock();
        }


    }
    public void print3(int loop) throws InterruptedException {
        lock.lock();
        try {
            if (flag!=2){
                B.await();
            }

            for (int i = 1; i <=3 ; i++) {

                System.out.println( Thread.currentThread().getName()+"   第"+i+"次" +"第"+loop+" 轮" );

            }

            flag=3;
            C.signal();
        }finally {
            lock.unlock();
        }


    }
    public void print4(int loop) throws InterruptedException {
        lock.lock();
        try {
            if (flag!=3){
                C.await();
            }

            for (int i = 1; i <=4 ; i++) {

                System.out.println( Thread.currentThread().getName()+"   第"+i+"次" +"第"+loop+" 轮"  );

            }

            flag=1;
            A.signal();
        }finally {
            lock.unlock();
        }


    }
}
public class ThreadsCommunication {


    public static void main(String[] args) {
        List<Object> list = new CopyOnWriteArrayList<>();

        shareResource r = new shareResource();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        r.print2(i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"AA").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        r.print3(i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"BB").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        r.print4(i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"CC").start();
    }
}
